import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "../store/user";

import NotFound from "../views/404.vue"; // 404页面
import Login from "../views/login/index.vue"; //登录页
import Home from "../views/home/index.vue"; //首页
import File from "../views/file/index.vue"; //文件
import VoiceToText from "../views/voiceToText/index.vue"; //录音实时转文字
import FileToText from "../views/fileToText/index.vue"; //文件转文字
import FileEdit from "../views/file/fileEdit.vue"; //文件编辑

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "Home",
      component: Home,
    },
    {
      path: "/404",
      name: "404",
      component: NotFound,
    },
    {
      path: "/login",
      name: "Login",
      component: Login,
    },
    {
      path: "/file",
      name: "File",
      component: File,
    },
    {
      path: "/fileEdit",
      name: "FileEdit",
      component: FileEdit,
    },
    {
      path: "/voiceToText",
      name: "VoiceToText",
      component: VoiceToText,
    },
    {
      path: "/fileToText",
      name: "FileToText",
      component: FileToText,
    }
  ],
});

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // const whiteList = ["/login", "/404"];
  // const userStore = useUserStore();
  // const token = userStore.getToken || localStorage.getItem("token");

  // if (!token) {
  //   // 未登录：允许进入白名单，否则重定向到登录
  //   if (whiteList.includes(to.path)) return next();
  //   return next("/login");
  // }

  // // 已登录：访问登录页时重定向到首页，其余正常放行
  // if (to.path === "/login") return next("/");
  return next();
});

export default router;
